/**
 * 删除数组元素
 * @param {Object} index 数组下标从0开始
 * @memberOf {TypeName} 
 * @return {TypeName} 
 */
Array.prototype.remove=function(dx) {
　　if(isNaN(dx)||dx>this.length){return false;}
　　for(var i=0,n=0;i<this.length;i++)
　　{
　　　　if(this[i]!=this[dx])
　　　　{
　　　　　　this[n++]=this[i]
　　　　}
　　}
　　this.length-=1
}

/** 默认配置 */
var _configs  = {
	save_url   : 'workflow/save.shtml',
	deploy_url : 'workflow/deploy.shtml',
	query_url  : 'workflow/flowXml.shtml',
	form_url   : 'forms/forms.shtml',//表单类型列表
	operate_url: 'operate/queryList.shtml',
	selector_url:'workflow/actorSelector.shtml',//节点参与者选择器面板
	dashLine   : null,//虚线	(鼠标移动画的虚线)
	startNode  : null,//画线开始节点
	endNode    : null,
	assignHandle : 'assignment-handler',// 节点任务指派人员对像,	// 节点任务指派人员默认类
	assignHandleClass   : '#{defaultAssigner}'
}  

//合并初始化属性
configs = $.extend(configs, _configs);


function closeSelectorWin(){
	// 隐藏参与者选择器窗口
	$('#selectorWin').window('close'); 
}

function saveSelectorWin(actorType, actorIds){
	$("#actorType").val(actorType);
	$("#actorIds").val(actorIds);
	modifyXmlNodeAttribute(configs.curXmlNode, 'actorType', actorType);
	modifyXmlNodeAttribute(configs.curXmlNode, 'actorIds', actorIds);
	// 隐藏参与者选择器窗口
	$('#selectorWin').window('close'); 	
}
	
$(function(){
	// 隐藏查看XML窗口
	$('#showXmlWin').window('close');
	closeSelectorWin();


	//获取XML并初始化流程图
	configs.workFlowId = $("#workFlowId").val();
	//configs.wfVersionId = $("#wfVersionId").val();
//	flowId = Ext.fly('workFlowId').getValue();
	initFlowXml(configs.workFlowId);
//	if (configs.wfVersionId != null){
//		
//	}
	//$("#south_tabs").tabs({});
	//为工具栏按钮注册事件
	$(".btn").click(function(){
		if(!$(this).hasClass('btn_down')) {
			configs.curBtn = $(this).attr('title');
			$('.btn_down').removeClass('btn_down');
			$(this).addClass('btn_down');
			//是否可以拖拽
			configs.canvas.forEach(function (el) {
				if(configs.curBtn == 'select') {
					el.drag(dragMove, dragStart, dragUp);
				} else {
					el.undrag();
				}
			});
		}
	});
	
	//流程图操作区注册事件
	$("#center").mousedown(function(){
		//console.log('----------center-------mousedown----------------------------------');
		//左键按下时方法
		resetVector();
		switch(configs.curBtn){
			case "select":	
				configs.curXmlNode = null;
				//showNodeParams(opts.nodeParams.process,"流程定义","picon01");
				//TODO lsx 设置流程属性
				break;
			//case "move": break;
			case "transition":
				//判断是否开始画线
				var cNode = getMousePositionNode();
				if(cNode != null && cNode.nodeType != 'end') {
					configs.startNode = cNode;
					configs.startNode.undrag();//屏蔽开始阶段拖拽功能
					document.onmousemove = lineMove;
				}
				break;
			default:
				addNode();
		}
		
	}).mouseup(function(){
		//左键释放时方法configs.curBtn != 'select' && (两个节点间连线)
		if(configs.curBtn=='transition' && configs.startNode != null){
			var cNode = getMousePositionNode();
			if(cNode != null){
				var tag = true;
				configs.endNode = cNode;
				if(configs.endNode.id == configs.startNode.id){//判断开始节点是否等于结束节点
					var tag = false;
				}
				//判断是否已经画过了
				for (var i = configs.connections.length; i--; ) {
					if(configs.connections[i].obj1.id==configs.startNode.id && configs.connections[i].obj2.id==configs.endNode.id){
						tag = false; break;
					}
				}
				
				if(tag){
					document.onmousemove = null;
					//画连接线
					var _path = addVectorPath(configs.startNode, configs.endNode);
					_path.arrPath.click(function(e){
						this.attr({fill: "#EEEEEE",'stroke':"red"});

						configs.curNode = _path;

						//连接线图标
						//configs.curXmlNode = getTtransitionXmlNode(this.nodeType, _curLine.obj1.title, _curLine.arrPath.title);  //(this.nodeType, this.title);
						configs.curXmlNode = getTtransitionXmlNode(_path.arrPath.nodeType, _path.obj1.title, _path.arrPath.title);
					    var x = (event.clientX-$("#center").scrollLeft()-35);
					    var y = (event.clientY-$("#center").scrollTop()-20);
					  //  console.log('x------'+x+'	$y-----------------------'+y);
						$('#line_operate').css("left", x).css("top", y).show();
					});			
					
					var _xmlNode = getXmlNode(configs.startNode.nodeType, configs.startNode.title);
					addXmlNode(_path.arrPath, _xmlNode);
				}
				if(configs.dashLine != null){
					configs.dashLine.remove();
				} 
				configs.startNode = null;
				configs.endNode = null;
		
			}
		}
		if(configs.curXmlNode == null) {
			//alert(5);
			 showNodeProperty();
		}
	});

	//栅格
	$("#gridBtn").click(function(){
		if($("#center").hasClass('center')){
			$("#center").removeClass('center')
		} else{
			$("#center").addClass('center')
		}
	});
	//查看XML、保存流程、流程部署
	$("#showXml").click(function(){
		$("#xmlTextArea").val(getXmlText());
		$('#showXmlWin').window('open');
	});

	// 保存流程  
	$("#saveFlow").click(function(){
//		var wfTypeId = $("wfTypeId").val();
		
		$.ajax({
			url:configs.save_url,
			type:"post",
			datatype:"json",
			data:{
		    	"flowXml"    : encodeURIComponent(getXmlText()), 
		    	"workFlowId" : configs.workflowId,
		    	"wftypeId"   : $("#wftypeId").val(),
		    	//"wfVersionId": configs.wfVersionId,
//		    	"respectOrgsId" : $("#respectOrgsId").val(),
		    	"formTypeId" : configs.xml.documentElement.getAttribute('forms'),
		    	"flowName"   : configs.xml.documentElement.getAttribute('name')
			},
			success:function(data){
			   	if(data.success){
				   //	configs.workflowId = data.workFlowId;
				   	//configs.wfVersionId = data.wfVersionId;
				   	$.messager.alert('信息提示','流程保存成功!','info');
				   //	top.closeDialog(true);
				}else{
					if (data.message != null && data.message != "") {
						alert(data.message);
					} else {
						alert("流程保存失败！");
					}
				}
			},
			error:function(XMLHttpRequest,textStatus,errorThrown){
				$.messager.alert('信息提示','流程保存失败!','info');
			}
		});
	});


	//部署流程 
	$("#deployFlow").click(function(){
//		var _wftype = Ext.get("wftype").dom.value;
//		var _respectOrgsId = Ext.get("respectOrgsId").dom.value;
		$.ajax({
			url:configs.deploy_url,
			type:"post",
			datatype:"json",
			data:{
		    	"flowXml"    : encodeURIComponent(getXmlText()), 
		    	"workFlowId" : configs.workFlowId,
		    	"wftypeId"   : $("#wftypeId").val(),
		    	//"wfVersionId": configs.wfVersionId,
//		    	"respectOrgsId" : $("#respectOrgsId").val(),
		    	"formTypeId" : configs.xml.documentElement.getAttribute('forms'),
		    	"flowName"   : configs.xml.documentElement.getAttribute('name')
			},
			success:function(data,textStatus){
			   	if(data.success){
				   	//configs.workflowId = data.workflowId;
				   	//configs.wfVersionId = data.wfVersionId;
				   	$.messager.alert('信息提示','流程部署成功!','info', function(){
				   		//parent.$(this).closeDialog();
				   	});
				   	
				}else{
					if (data.message != null && data.message != "") {
						alert(data.message);
					} else {
						alert("流程保存失败！");
					}
				}
			},
			error:function(XMLHttpRequest,textStatus,errorThrown){
				$.messager.alert('信息提示','流程部署失败!','info');
			}
		});

	});
	
	$("#saveXml").click(function(){
		//初始化全局变量
		initProcess();
		xmlToProcess($("#xmlTextArea").val());
		initGraphClick();
		$('#showXmlWin').window('close');
	});

	$("#leftRight").click(function(){
		//console.log('-----------topDown----------click-------------------');
		ruleLine("leftRight");
	});	
	
	$("#topDown").click(function(){
		//console.log('-----------topDown----------click-------------------');
		ruleLine("topDown");
	});
	
	$("#borkenLine").click(function(){
		//console.log('-----------borkenLine----------click-------------------');
		ruleLine("borkenLine");
	});
	
	$("#borkenLine1").click(function(){
		//console.log('-----------borkenLine1----------click-------------------');
		ruleLine("borkenLine1");
	});

	$("#normalLine").click(function(){
		var sNode = configs.curNode.obj1;
		var eNode = configs.curNode.obj2;
		modifyXmlNodeAttribute(configs.curXmlNode, "g", "");
		
		configs.curNode.arrPath.remove();
		//addVectorPath(sNode, eNode, "");
		var _path = addVectorPath(sNode, eNode, "");
		_path.arrPath.click(function(e){
			this.attr({fill: "#EEEEEE",'stroke':"red"});

			configs.curNode = _path;

			//连接线图标
			//configs.curXmlNode = getTtransitionXmlNode(this.nodeType, _curLine.obj1.title, _curLine.arrPath.title);  //(this.nodeType, this.title);
			configs.curXmlNode = getTtransitionXmlNode(_path.arrPath.nodeType, sNode.title, _path.arrPath.title);
		    var x = (event.clientX-$("#center").scrollLeft()-35);
		    var y = (event.clientY-$("#center").scrollTop()-20);
		  //  console.log('x------'+x+'	$y-----------------------'+y);
			$('#line_operate').css("left", x).css("top", y).show();
		});
		$('#line_operate').hide();
	});
	
	//删除矢量图路径
	$("#delLine").click(function(){
		removeLine();
		//getStartEndNodeByLine(endNodeName);
		
	});
	//删除节点
	$("#delNode").click(function(){
		if(configs.curNode != null) {
			
			//1、删除XML节点及XML几点连线
			var nodes = configs.xml.getElementsByTagName('transition');
			if(nodes.length > 1){
				for(i=0; i<nodes.length; i++){
					if(nodes[i].getAttribute("name") == configs.curNode.title){
						nodes[i].parentNode.removeChild(nodes[i]);
					}					
				}	
			}
			
			configs.curXmlNode.parentNode.removeChild(configs.curXmlNode);
			
			//removeXmlNode(getXmlNode(configs.curNode.nodeType, configs.curNode.title));
			
			//console.log('---------------删除矢量节点及矢量节点连线--------------');
			//1、删除矢量节点及矢量节点连线
			for (var i = configs.connections.length; i--; ) {
				//console.log('configs.connections[i].obj1.title=='+configs.connections[i].obj1.title);
				//console.log('configs.connections[i].obj2.title=='+configs.connections[i].obj2.title);
				//console.log('configs.curNode.title=='+configs.curNode.title);
				if(configs.connections[i].obj1.title==configs.curNode.title || configs.connections[i].obj2.title == configs.curNode.title){
					//console.log('--------------删除线 i-----------'+i);
					configs.connections[i].arrPath.remove();
					configs.connections.remove(i);
				}
			}
			$("#"+configs.curNode.id).remove();
			configs.curNode.remove();
		}
		
		$("#node_operate").hide();

	});
	
	$("#center").parent().addClass('center');
	$("#center").parent().scroll(function(){
		
		//console.log('------------center-----scroll-------------------------');
		configs.canvas.forEach(function (el) {
			if(el.type=='rect'){
				var divP = getNodeDivPosition(el.nodeX, el.nodeY, el.nodeType);
				//console.log('el.title==='+el.title+'divP.x----'+divP.x+'	&&divP.y===='+divP.y);
				//重新定位HTML位置
				$('#'+el.id).css("left", divP.x).css("top", divP.y);
			} 
		});
		
	});
});

function removeLine(){
	//console.log('-----------------------removeLine------------------------');
	if(configs.curNode != null) {
		//1、移除XML连线
		var traXmlNode = getTtransitionXmlNode(configs.curNode.arrPath.nodeType, configs.curNode.obj1.title, configs.curNode.arrPath.title);
		//console.log(traXmlNode);
		
		traXmlNode.parentNode.removeChild(traXmlNode);
		//console.log('-------removeXmlNode---------traXmlNode--------------');
		//console.log(traXmlNode);
		//2、移除矢量图连线
		for (var i = configs.connections.length; i--; ) {
			//console.log(configs.connections[i]);	
			if(configs.connections[i].obj1.id==configs.curNode.obj1.id && configs.connections[i].obj2.id==configs.curNode.obj2.id){
				configs.connections.remove(i);
			}
			
		}
		configs.curNode.arrPath.remove();
		configs.curNode = null;
		configs.curXmlNode = null;
		//console.log('````````````````````````````````````````````````````````````````````');
//		for (var i = configs.connections.length; i--; ) {
//			console.log(configs.connections[i]);	
//		}
//		console.log('```````````````````````````````end`````````````````````````````````````');
	}

	$('#line_operate').hide();
}

//初始设置流程定义
function initProcess(){	
	//初始化全局变量
	configs.xml = null;
	$("#center").empty();
	configs.canvas = new Raphael("center", 2000, 400);//创建绘图对象
	//创建xml对象
	createXml();

	
	//showNodeParams(opts.nodeParams.process,"流程定义","picon01");
};

/*****************************矢量图******************************************/
//拖动节点中事件
var dragMove = function (dx, dy) {
	var att = { x: this.ox + dx, y: this.oy + dy };
	this.attr(att);
	  
	var dp = getNodeDivPosition(this.ox + dx, this.oy + dy, this.nodeType);
	$('#'+this.id).css("left", dp.x).css("top", dp.y);
	for (var i = configs.connections.length; i--; ) {
		configs.canvas.drawArr(configs.connections[i]);	
	}
};

//拖动节点开始时的事件
var dragStart = function () {
	this.ox = this.attr("x");
	this.oy = this.attr("y");
	this.animate({ "fill-opacity": 100 }, 500);
};           

//拖动节点结束后的事件
var dragUp = function () {
	//var _opsition = this.getBBox();
	this.animate({ "fill-opacity": 100 }, 500);
	this.nodeX=this.attr("x");this.nodeY=this.attr("y");
	//修改XML节点位置属性
	var _g = this.attr("x")+","+this.attr("y")+",110,50";
	var _xmlNode = getXmlNode(this.nodeType, this.title);//findXmlNode(this);
	modifyXmlNodeAttribute(_xmlNode, "g", _g);

};



/**
 * 获取画布上鼠标位置矢量图形
 * @return 矢量图形 
 */
function getMousePositionNode(){
	var cNode = null;
	var cmp = getCanvasMousePosition();

	configs.canvas.forEach(function (el) {
		var _bbox = el.getBBox();
		if(_bbox.x < cmp.x && cmp.x < _bbox.x2 && _bbox.y< cmp.y && cmp.y<_bbox.y2){
			cNode = el; 
		}
	});

	return cNode;
}

/**
 * 获取当前画布鼠标位置
 * @returns
 */
function getCanvasMousePosition(){

	//当前鼠标位置+滚动条位置-画布在html偏移位置
    return {
        x: event.clientX + $("#center").scrollLeft() - configs.canvasLeft,
        y: event.clientY + $("#center").scrollTop()- configs.canvasTop
    };
}

/**
 * 两个矢量图连线鼠标移动方法
 * @param e
 * @returns
 */
function lineMove(e){
	
	if(configs.startNode != null){
		if(configs.dashLine != null){
			configs.dashLine.remove();
		} 
		var _location = configs.startNode.getBBox();
		var x1 = _location.x+_location.width/2;
		var y1 = _location.y+_location.height/2;
		var cmp = getCanvasMousePosition();
		var _path ="M"+(x1) +","+(y1)+" L"+cmp.x+", "+cmp.y +"z";
	
		configs.dashLine = configs.canvas.path(_path).attr({stroke: "#F0F", "stroke-dasharray":"- ",'stroke-width':'2'}); 
	}
}

/**
 * 重置矢量图颜色(去除鼠标选择图形红色)
 */
function resetVector(){
	$('#line_operate').hide();
	configs.canvas.forEach(function (el) {
		if(el.type=='rect'){
			el.attr({fill: "#F6F7FF",'stroke':"#27548D"});
		} else if(el.type=='path') {
			el.attr({fill: "#none",'stroke':"#808080"});
		}
	});
}

/**
 * 新增矢量节点
 * @param e
 * @returns {Boolean}
 */
function addNode(){ 

	//如果start存在不再进行添加矢量节点
	if(configs.curBtn == "start" && configs.canvas.getById('start')){
		return false;
	}

	var mp = getCanvasMousePosition();
	var _node = addVectorNode(mp.x-configs.node.width/2, mp.y-configs.node.height/2, configs.curBtn, null);

	_node.drag(dragMove, dragStart, dragUp);
	_node.click(function(e){
		this.attr({'fill': "#F6F7FF",'stroke':"red"});
		configs.curNode = this;
		configs.curXmlNode = getXmlNode(this.nodeType, this.title);
		//console.log('当前XML节点=551==='+configs.curXmlNode);
		//当前XML节点
		showNodeProperty();
	});	
	//补充html点击事件BUG
	$("#"+_node.id).click(function(){
		var nodeid = $(this).attr('id');
		configs.canvas.forEach(function (el) {
			//console.log('--node.click--------el.id=='+el.id+'	&&nodeid=='+nodeid);
			if(el.id == nodeid){
				el.attr({'fill': "#F6F7FF",'stroke':"red"});
				configs.curNode = el;
				configs.curXmlNode = getXmlNode(el.nodeType, el.title);
				//console.log('当前XML节点=552==='+configs.curXmlNode);
				//当前XML节点
				showNodeProperty();
			}
		});
		
	});
	
	
	addXmlNode(_node);//添加XML节点


}


function initGraphClick(){
	configs.canvas.forEach(function (el) {
		if(el.type=='rect'){
			el.drag(dragMove, dragStart, dragUp);
			el.click(function(e){
				
				this.attr({'fill': "#F6F7FF",'stroke':"red"});
				configs.curNode = this;
				configs.curXmlNode = getXmlNode(this.nodeType, this.title);
			    //var x = (configs.curNode.nodeX+15);
			    //var y = (configs.curNode.nodeY-28);
			    var divP = getNodeDivPosition(configs.curNode.nodeX, configs.curNode.nodeY, configs.curNode.nodeType);
			    
			  //  console.log('x------'+x+'	$y-----------------------'+y);
				$('#node_operate').css("left", divP.x+246).css("top", divP.y-2).show();
				
				//console.log('当前XML节点===='+configs.curXmlNode);
				//当前XML节点
				showNodeProperty();
			});	
			//补充html点击事件BUG
			$("#"+el.id).click(function(){
				var nodeid = $(this).attr('id');
				configs.canvas.forEach(function (el) {
					//console.log('--node.click--------el.id=='+el.id+'	&&nodeid=='+nodeid);
					if(el.id == nodeid){
						el.attr({'fill': "#F6F7FF",'stroke':"red"});
						configs.curNode = el;
						configs.curXmlNode = getXmlNode(el.nodeType, el.title);
						
						//var divP = getNodeDivPosition(nodeX, nodeY, nodeType);
					  //  console.log('x------'+x+'	$y-----------------------'+y);
						//$('#node_operate').css("left", x).css("top", y).show();
						
						//console.log('当前XML节点=22==='+configs.curXmlNode);
						//当前XML节点
						showNodeProperty();
					}
				});
				
			});
		} 
	});
	
	//连接线点击事件
	for (var i=0; i < configs.connections.length; i++) {
		//var _path = connections[i];
		
		(function(i){ //闭包
			var _curLine = configs.connections[i];
			configs.connections[i].arrPath.click(function(e){
				this.attr({fill: "#EEEEEE",'stroke':"red"});
				//var _curLine = _cur;
				
				configs.curNode = _curLine;
				//console.log('---------------line  click-----------------------------------------');
				//console.log(e);
//				console.log('this.nodeType==='+this.nodeType+'	&&sNode.title=='+_curLine.obj1.title
//						+'	&&_path.arrPath.title====='+ _curLine.arrPath.title);
				//连接线图标
				configs.curXmlNode = getTtransitionXmlNode(this.nodeType, _curLine.obj1.title, _curLine.arrPath.title);  //(this.nodeType, this.title);
				
			    var x = (event.clientX-$("#center").scrollLeft()-35);
			    var y = (event.clientY-$("#center").scrollTop()-20);
			  //  console.log('x------'+x+'	$y-----------------------'+y);
				$('#line_operate').css("left", x).css("top", y).show();
				/*
				var c_div = document.getElementById('main-panel-body');
			    var x = (event.clientX-c_div.scrollLeft-35);
			    var y = (event.clientY-c_div.scrollTop-20);
			    
				Ext.fly('line_operate').show().setXY([x, y]);
				*/
				
			});
		})(i);

	}
}


/*********************************XML操作**********************************************************************/



//添加XML节点属性属性
function addXmlNodeAttribute(xmlNode, name, value){
	var attribute = configs.xml.createAttribute(name);
	attribute.value = value;
	xmlNode.setAttributeNode(attribute);
	return xmlNode;
}

//修改XML节点属性
function modifyXmlNodeAttribute(xmlNode, name, value){
	if(xmlNode != null){
		xmlNode.setAttribute(name, value);
		return xmlNode;
	}		
}

/**
 * 添加XML子节点
 * @param parentNode xml父类节点
 * @param nodeName 节点名称
 * @return 子节点
 */
function addXmlChildNode(parentXmlNode, nodeName){	 
	var newXmlNode = configs.xml.createElement(nodeName);
	parentXmlNode.appendChild(newXmlNode);
	return newXmlNode;
}

/**
 * 添加XML节点
 * @param node 矢量图节点对象
 * @param parentXmlNode
 */
function addXmlNode(node, parentXmlNode){
	
	if(parentXmlNode == null) {//没有父类节点默认指向根节点
		parentXmlNode = configs.xml.documentElement;
	}
	
	var newXmlNode = null;//新XML节点
	var attr = null;//属性
	var childXmlNode = null;//子节点
	
	switch(node.nodeType){
		case "start":
			newXmlNode = configs.xml.createElement("start");
			break;
		case "end":
			newXmlNode = configs.xml.createElement("end");
			break;
		case "task":
			newXmlNode = configs.xml.createElement("task");
			addXmlNodeAttribute(newXmlNode, "actorType", "");
			addXmlNodeAttribute(newXmlNode, "actorIds", "");
			addXmlNodeAttribute(newXmlNode, "operate","");
			addXmlNodeAttribute(newXmlNode, "forms","");
			addXmlNodeAttribute(newXmlNode, "recover","0");
			addXmlNodeAttribute(newXmlNode, "timeLimit","5");
			addXmlNodeAttribute(newXmlNode, "dynamicSelect","0");
			addXmlNodeAttribute(newXmlNode, "dynamicJump","0");
			addXmlNodeAttribute(newXmlNode, "trunTodo","0");
			//添加子节点
			childXmlNode = addXmlChildNode(newXmlNode, configs.assignHandle);
			addXmlNodeAttribute(childXmlNode, "expr", configs.assignHandleClass);
			break;
		case "subPro":
			newXmlNode = configs.xml.createElement("subPro");
			//addXmlNodeAttribute(newXmlNode,"sub-process-key","");
			break;
		case "sign":
			newXmlNode = configs.xml.createElement("sign");
			addXmlNodeAttribute(newXmlNode, "actorType", "");
			addXmlNodeAttribute(newXmlNode, "actorIds", "");
			addXmlNodeAttribute(newXmlNode, "operate", "");
			//添加子节点
//			childXmlNode = addXmlChildNode(newXmlNode, opts.assignHandle);
//			addXmlNodeAttribute(childXmlNode, "expr", opts.signClass);
			break;
		case "join":
			newXmlNode = configs.xml.createElement("join");
			break;
		case "fork":
			newXmlNode = configs.xml.createElement("fork");	
			break;
		case "transition":
			//添加子节点
			childXmlNode = addXmlChildNode(parentXmlNode, "transition");
			//当选择节点的文本值为null，包括的节点有(join,fork)
			//console.log('add transition start------------------>');
			addXmlNodeAttribute(childXmlNode, "name", "to "+configs.endNode.title);
			addXmlNodeAttribute(childXmlNode, "to", configs.endNode.title);
			//console.log('add transition end------------------>');
			break;
		case "decision":
			newXmlNode = configs.xml.createElement("decision");
			break;
	}
	if(node.nodeType != "transition"){
		addXmlNodeAttribute(newXmlNode, "id",node.id);
		addXmlNodeAttribute(newXmlNode, "name",node.title);
		addXmlNodeAttribute(newXmlNode, "g",node.attrs.x+","+node.attrs.y+",110,50");
		parentXmlNode.appendChild(newXmlNode);
	}
}


/**
 * 获取XML节点
 * @param nodeType 节点类型
 * @param title 节点标题
 * @return 
 */
function getXmlNode(nodeType, title){
	
	if(title == null || nodeType == null) return null;
	
	var nodes = configs.xml.getElementsByTagName(nodeType);
	if(nodes.length == 1){
		return nodes[0];
	} else {
		for(i=0; i<nodes.length; i++){
			if(nodes[i].getAttribute("name") == title){
				return nodes[i];
			}					
		}	
	}

	return null;
}




function getTtransitionXmlNode(nodeType, title, traTitle){
	var nodes = configs.xml.getElementsByTagName(nodeType);
	//traTitle = traTitle.substring(3, traTitle.length);
	if(nodes.length > 1){
		for(i=0; i<nodes.length; i++){
			
			if(nodes[i].getAttribute("name") == traTitle && nodes[i].parentNode.getAttribute("name") == title){
				return nodes[i];
			}					
		}	
	}
	
	return null;
}

/**
 * 获取XML子节点根据父类节点查找
 * @param nodeType 节点类型
 * @param parentNode 父类节点
 */
function getXmlChildNodeByType(nodeType, parentNode){
	return parentNode.getElementsByTagName(nodeType);
	
}
/**
function removeXmlNode(xmlNode){
	configs.xml.documentElement.removeChild(xmlNode);
}*/

function removeXmlNode(nodeName){
	
	configs.xml.documentElement.removeChild(xmlNode);
}

//根据XML获取XML字符串
function getXmlText(){
	//console.log('----------getXmlText-------------------');
	var xmlText;
	if(window.ActiveXObject){

		xmlText = configs.xml.xml.replace(/></g,'>\n\r<');
		xmlText = xmlText.replace(/xmlns=\"\"/g,'');

	} else if(document.implementation && document.implementation.createDocument){
		var xmls = new XMLSerializer(); 
		xmlText = xmls.serializeToString(configs.xml); 
		//xmlText = xmlText.replace(/></g,'>\n\r<').replace(/xmlns=\"\"/g,'').replace('\n',"");; 
	}
	//console.log(configs.xml);
	return xmlText;
}


/** 
 * 初始化流程图
 * 根据流程定义主键获取文件内容并读取展示 */
function initFlowXml(workFlowId) {
	var workFlowId = parseInt(workFlowId);
	//console.log(workFlowId);
	//初始化全局变量
	initProcess();
	if(!isNaN(workFlowId)) {
		$.ajax({
			url:configs.query_url,
			type:"post",
			data:{"workFlowId":workFlowId},
			datatype:"json",
			success:function(data,textStatus){
				xmlToProcess(data);
				initGraphClick();
			},
			complete:function(XMLHttpRequest,textStatus){
			},
			error:function(XMLHttpRequest,textStatus,errorThrown){
				$.messager.alert('信息提示','初始化流程失败!','info');
			}
		});
	}

}

